package com.baike.controller;

import com.baike.data.RedisName;
import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbStudentHave;
import com.baike.feign.TbDiscountsFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("DiscountBusinessController")
@Api("拼团课程 业务调度实例")
public class DiscountBusinessController {
    @Autowired
    private TbDiscountsFeign discountsFeign;
    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping
    @ApiOperation("增加用户拼团购买关联数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户Id",name = "sId",required = true,paramType = "query"),
            @ApiImplicitParam(value = "课程Id",name = "lessonId",required = true,paramType = "query")
    })
    public Result postUserHave(@RequestParam("sId")Long sId ,@RequestParam("lessonId")Long lessonId){
        StringRedisSerializer serializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jack = new Jackson2JsonRedisSerializer(Object.class);
        redisTemplate.setKeySerializer(serializer);
        redisTemplate.setHashKeySerializer(serializer);
        redisTemplate.setValueSerializer(jack);
        redisTemplate.setHashValueSerializer(jack);
        redisTemplate.setValueSerializer(serializer);
        //团购缓存 大K
        BoundHashOperations hashOps = redisTemplate.boundHashOps(RedisName.GROUP_PURCHASE_PARENT);
        //存放每个课程的购买人ID
        BoundListOperations ops = redisTemplate.boundListOps(RedisName.GROUP_PURCHASE_LIST + lessonId);

        if(Objects.nonNull(sId) && Objects.nonNull(lessonId)){
            TbStudentHave have = new TbStudentHave();
            have.setStudentId(sId).setCourseId(lessonId);
            Result<ConstantEnum> result = discountsFeign.postHave(sId,lessonId);
            if(result.getCode().equals(2000)){
                // 如果成功 添加数据
                //判断是否已经有K
                if(hashOps.hasKey(RedisName.GROUP_PURCHASE_BOY + lessonId)){
                    // 已经存在 自增 加1
                    hashOps.increment(RedisName.GROUP_PURCHASE_BOY + lessonId , 1);
                    // 添加购买人数据
                    ops.rightPush(lessonId + "." + sId);

                }else {
                    // 不存在 设置为 1
                    hashOps.put(RedisName.GROUP_PURCHASE_BOY + lessonId,1L);
                    // 添加购买人数据
                    ops.leftPush(lessonId + "." + sId);
                }


                return Result.build(ConstantEnum.SUCCESS);
            }else {
                return Result.build(ConstantEnum.ERROR);
            }

        }
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }


}
